package ChatEntry;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChatFrame extends JFrame {
    private String nickname;
    private JTextArea chatArea;
    private JTextField messageField;
    private DefaultListModel<String> onlineUsersModel;

    public ChatFrame() {
        this("匿名用户");
    }
    public ChatFrame(String nickname) {
        this.nickname = nickname;
        initializeUI();
        setupEvents();
    }

    private void initializeUI() {
        setTitle("局域网聊天 - " + nickname);
        setSize(900, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示

        // 主面板使用边界布局
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        mainPanel.setBackground(new Color(240, 245, 250));

        // 创建分割面板，左侧聊天区域，右侧在线用户
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setDividerLocation(600); // 设置初始分割位置
        splitPane.setDividerSize(5);
        splitPane.setBackground(new Color(240, 245, 250));

        // 左侧聊天区域
        JPanel chatPanel = createChatPanel();
        splitPane.setLeftComponent(chatPanel);

        // 右侧在线用户区域
        JPanel onlinePanel = createOnlineUsersPanel();
        splitPane.setRightComponent(onlinePanel);

        // 底部消息发送区域
        JPanel inputPanel = createInputPanel();

        // 添加组件到主面板
        mainPanel.add(splitPane, BorderLayout.CENTER);
        mainPanel.add(inputPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    private JPanel createChatPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 聊天区域标题
        JLabel chatLabel = new JLabel("聊天消息");
        chatLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        chatLabel.setBorder(new EmptyBorder(5, 5, 10, 5));

        // 聊天内容区域
        chatArea = new JTextArea();
        chatArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        chatArea.setEditable(false);
        chatArea.setLineWrap(true); // 自动换行
        chatArea.setWrapStyleWord(true); // 按单词换行
        chatArea.setBackground(Color.WHITE);

        JScrollPane scrollPane = new JScrollPane(chatArea);
        scrollPane.setBorder(BorderFactory.createLineBorder(new Color(180, 180, 180)));

        panel.add(chatLabel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createOnlineUsersPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 在线用户标题
        JLabel onlineLabel = new JLabel("在线用户");
        onlineLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        onlineLabel.setBorder(new EmptyBorder(5, 5, 10, 5));

        // 在线用户列表
        onlineUsersModel = new DefaultListModel<>();
        // 添加一些示例用户
        addOnlineUser(nickname); // 当前用户
//        addOnlineUser("系统消息");
//        addOnlineUser("张三");
//        addOnlineUser("李四");

        JList<String> onlineUsersList = new JList<>(onlineUsersModel);
        onlineUsersList.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        onlineUsersList.setBorder(BorderFactory.createLineBorder(new Color(180, 180, 180)));
        onlineUsersList.setBackground(Color.WHITE);

        JScrollPane scrollPane = new JScrollPane(onlineUsersList);

        panel.add(onlineLabel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createInputPanel() {
        JPanel panel = new JPanel(new BorderLayout(10, 0));

        // 消息输入框
        messageField = new JTextField();
        messageField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        messageField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(180, 180, 180)),
                new EmptyBorder(5, 5, 5, 5)));

        // 发送按钮
        JButton sendButton = new JButton("发送");
        sendButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        sendButton.setPreferredSize(new Dimension(80, 35));
        sendButton.setBackground(new Color(66, 133, 244));
        sendButton.setForeground(Color.WHITE);
        sendButton.setBorder(BorderFactory.createEmptyBorder());
        sendButton.setFocusPainted(false);

        panel.add(messageField, BorderLayout.CENTER);
        panel.add(sendButton, BorderLayout.EAST);

        // 绑定发送按钮事件
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        // 绑定回车键发送消息
        messageField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        return panel;
    }

    private void setupEvents() {
        // 可以在这里添加其他事件监听器
    }

    private void sendMessage() {
        String message = messageField.getText().trim();
        if (!message.isEmpty()) {
            // 获取当前时间
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String time = sdf.format(new Date());

            // 在聊天区域添加消息
            chatArea.append("[" + time + "] " + nickname + ": " + message + "\n");

            // 清空输入框
            messageField.setText("");

            // 滚动到底部
            chatArea.setCaretPosition(chatArea.getDocument().getLength());
        }
    }

    public void addOnlineUser(String username) {
        onlineUsersModel.addElement(username);
    }

    public void removeOnlineUser(String username) {
        onlineUsersModel.removeElement(username);
    }

    public static void main(String[] args) {
        // 示例：以"测试用户"身份进入聊天
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChatFrame("测试用户").setVisible(true);
            }
        });
    }
}
